package IO流.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

public class Img1 {
    public static void main(String[] args) throws Exception{
        //建立缓冲图像  设置面板宽度
        //1是真彩，2是透明度，10是灰度
         BufferedImage i=new BufferedImage(500,500,1);//
         var g=i.createGraphics();//根据图像建立2D画笔
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        //红绿蓝 透明度
        g.setColor(new Color(255,0,0,255));
        //画一个矩形  用设置的颜色进行填充  要和缓冲图像大小对齐，不要让面板漏出来
        g.fillRect(0,0,500,500);

        //一定要注意书写的顺序，先粗细，后颜色，在画线
        //设置画的粗细
        g.setStroke(new BasicStroke(1));
        //设置线条的颜色
        g.setColor(Color.blue);
        //设置线条  值对应为起始位置，和终止位置
        g.drawLine(250,0,250,500);
        g.drawLine(0,250,500,250);
        //画正方形
        //g.drawRect(125,125,250,250);
        //设置颜色，红蓝绿透明色
        g.setColor(new Color(0,55,33,150));
        g.fillRect(150,150,200,200);

        Random rand=new Random();
        g.setColor(Color.YELLOW);
        g.drawLine(rand.nextInt(500), rand.nextInt(500), rand.nextInt(500), rand.nextInt(500));
        g.drawLine(rand.nextInt(500), rand.nextInt(500), rand.nextInt(500), rand.nextInt(500));
        g.drawLine(rand.nextInt(500), rand.nextInt(500), rand.nextInt(500), rand.nextInt(500));
        g.drawLine(rand.nextInt(500), rand.nextInt(500), rand.nextInt(500), rand.nextInt(500));

        //画多边形
        g.setColor(new Color(255,255,0));
        Polygon p=new Polygon();//多边形
        p.addPoint(10,20);
        p.addPoint(20,40);
        p.addPoint(10,40);
        p.addPoint(20,20);
        g.drawPolygon(p);

        g.dispose();//释放画笔
        ImageIO.write(i,"png",new File("image02.png"));
        //调用系统命令 打开图像
        Runtime.getRuntime().exec("cmd /k start F:\\IDEA\\maven-String\\image02.png ");

    }
}
